package me.moscrop.RootCommand.commands;

import me.moscrop.RootCommand.utilities.Chat;
import me.moscrop.RootCommand.utilities.Utility;
import me.moscrop.RootCommand.utilities.MV;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class MultiWorld {
	
	public enum arguments
	{
		NEW, DELETE, LIST, SETSPAWN, NULLVALUE;

		private static arguments available(String str)
	    {
	        try {
	            return valueOf(str.toUpperCase());
	        } 
	        catch (Exception ex) {
	            return NULLVALUE;
	        }
	    }   
	}
	
	public static Boolean join(CommandSender sender, Command command, String label, String[] args) {

		String targetWorld;
		Player targetPlayer;
		
		if (args.length == 1) {
			targetWorld = args[0];
			targetPlayer = (Player) sender;
		} else if (args.length == 2) {
			targetWorld = args[0 ];
			targetPlayer = sender.getServer().getPlayer(args[0]);
		} else {
			return false;
		}
		
		World world = sender.getServer().getWorld(targetWorld);
		
		if (world != null) {
			Location location = world.getSpawnLocation();	
			targetPlayer.teleport(Utility.getSafeLocationAt(location));	
			Chat.messageAll(targetPlayer, ChatColor.DARK_GRAY + " has joined " + ChatColor.WHITE + targetWorld);
		}else{
			sender.sendMessage(ChatColor.RED + "World not found!");
		}
		
		return true;
	}
	
	
	public static Boolean world(CommandSender sender, Command command, String label, String[] args) {
		String option;
		String worldName	= null;
		String worldType	= null;
		String seed 		= null;
		String generator	= null;
		Player player 		= (Player) sender;
		
		if (args.length == 1 ) {
			option 		= args[0];
		} else if(args.length == 2) {
			option 		= args[0];
			worldName 	= args[1];
			worldType	= args[2].toUpperCase();
		} else if(args.length == 3) {
			option		= args[0];
			worldName	= args[1];
			worldType	= args[2].toUpperCase();
		} else {
			option		= args[0];
			worldName	= args[1];
			worldType	= args[2].toUpperCase();
			seed 		= args[3];
		}
		
		switch (arguments.available(option)) {		
			case NEW:	
				boolean added = MV.addworld(worldName, MV.getEnvFromString(worldType), seed, generator);
				//if (added) {
				//	Chat.message(player, "World " + worldName + " created!");			
				//}		
			return added;
			
			case DELETE:				
				boolean succ = sender.getServer().unloadWorld(worldName, false);
				if (succ) {
					Chat.messageAll(player, ChatColor.DARK_GRAY + " has deleted " + ChatColor.WHITE + worldName);
				}
			return succ;
			
			case SETSPAWN:
					MV.setSpawn(player);
					Chat.message(player, "new spawn set");
				return true;
				
			case LIST:			
				for (World worlds : Bukkit.getServer().getWorlds()) {	
					Chat.message(player, worlds.getName());
				}		
			return true;
			
			default:
			return false;	
		}
	}
	

	
	
}
